package com.zslc.securitymonitoring.websocket;

import com.zslc.securitymonitoring.service.AlarmTypeService;
import com.zslc.securitymonitoring.service.RealTimeService;
import com.zslc.securitymonitoring.service.VulService;
import com.zslc.securitymonitoring.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 今日事件定时任务
 */
@EnableScheduling
@Component
@Slf4j
public class RealtimeTask {
    @Autowired
    private RealTimeService realtimeService;
    @Autowired
    private AlarmTypeService alarmTypeService;
    @Resource
    private WebSocketServer webSocketServer;
    @Autowired
    private VulService vulService;

    //5min执行一次
    @Scheduled(fixedDelay = 1000 * 60 * 5)
    public void realtimeEventTask() {
        Map<String, Object> map = new HashMap<>();
        try {
            //今日事件
            List<GlobalSeverityVO> list = realtimeService.listBy1Day();
            map.put("event", list);
            webSocketServer.broadCastInfo(map);

            //事件报警中心
            List<StatusStatistic> statusList = realtimeService.statusStatistic();
            map.clear();
            map.put("status", statusList);
            webSocketServer.broadCastInfo(map);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    //5min执行一次
    @Scheduled(fixedDelay = 1000 * 60 * 5)
    public void regionDisplayTask() {
        try {
            //地图分区域展示
            List<RegionStatistic> list = alarmTypeService.regionStatistic();
            Map<String, List> map = new HashMap<>();
            map.put("region", list);
            webSocketServer.broadCastInfo(map);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }


    //一天执行一次
    @Scheduled(cron = "0 0 23 * * ?")
    public void PerDayUpdateTask() {
        Map<String, Object> map = new HashMap<>();
        try {
            //过去六个月告警总览
            Map<String, List<GlobalSeverityStatistic>> resultMap = alarmTypeService.severityStatisticBy6Month();
            map.put("seversity", resultMap);
            webSocketServer.broadCastInfo(map);

            //过去六个月告警类型分布
            List<Map<String, Object>> list = alarmTypeService.typeStatisticBy6Month();
            map.clear();
            map.put("type", list);
            webSocketServer.broadCastInfo(map);

            //过去六个月安全漏洞分布
            List<VulStatisticVO> vulMap = vulService.vulStatisticBy6Month();
            map.clear();
            map.put("vul", vulMap);
            webSocketServer.broadCastInfo(map);

            //30天内报警数量top10
            List<UrlStatistic> urlList = alarmTypeService.urlStatisticBy30Day();
            map.clear();
            map.put("url", urlList);
            webSocketServer.broadCastInfo(map);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

}
